package com.ngm.tea.t.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ngm.tea.common.PageInfo;
import com.ngm.tea.t.dao.PrivilegeDao;
import com.ngm.tea.t.entity.Privilege;
import com.ngm.tea.t.service.PrivilegeService;

/**
 * 服务层
 * @author zjw
 * @version 1.0
 * @date 2016-09-01 21:07:07
 *
 */
@Service("privilegeService")
public class PrivilegeServiceImpl implements PrivilegeService {
	
	@Resource
	private PrivilegeDao privilegeDao;

	public PrivilegeDao getPrivilegeDao() {
		return privilegeDao;
	}

	@Override
	public void insert(Privilege privilege) {
		privilegeDao.insert(privilege);
	}

	@Override
	public void deleteById(Long id) {
		privilegeDao.deleteById(id);
	}

	@Override
	public void deleteByIds(String ids) {
		String[] _ids = ids.split(",");
		if (_ids.length > 0) {
			List<String> idlist = Arrays.asList(_ids);
			privilegeDao.deleteByIds(idlist);
		}
	}

	@Override
	public void delete(Privilege privilege) {
		privilegeDao.delete(privilege);
	}

	@Override
	public void update(Privilege privilege) {
		privilegeDao.update(privilege);
	}

	@Override
	public Privilege selectOne(Long id) {
		return privilegeDao.selectOne(id);
	}

	@Override
	public List<Privilege> selectAll() {
		return privilegeDao.selectAll();
	}

	@Override
	public List<Privilege> selectList(Privilege privilege, PageInfo pageInfo) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(privilege));
		map.put("pageInfo", pageInfo);
		map.put("order", pageInfo.getOrderField()+" "+pageInfo.getOrderDirection());
		return privilegeDao.selectList(map);
	}

	@Override
	public Long count(Privilege privilege) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(privilege));
		return privilegeDao.count(map);
	}
	
	public String makeStr(Privilege privilege) {
		StringBuffer sb = new StringBuffer();
		if(!StringUtils.isEmpty(privilege.getId())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("id LIKE '%" + privilege.getId()+"%'");
			} else {
				sb.append(" AND id LIKE '%" + privilege.getId()+"%'");
			}
		}
		if(!StringUtils.isEmpty(privilege.getPriCode())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("priCode LIKE '%" + privilege.getPriCode()+"%'");
			} else {
				sb.append(" AND priCode LIKE '%" + privilege.getPriCode()+"%'");
			}
		}
		if(!StringUtils.isEmpty(privilege.getPriName())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("priName LIKE '%" + privilege.getPriName()+"%'");
			} else {
				sb.append(" AND priName LIKE '%" + privilege.getPriName()+"%'");
			}
		}
		if (StringUtils.isEmpty(sb.toString())) {
			return null;
		} else {
			return sb.toString();
		}
	}
	
}